<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class VideoDB extends BaseDB
{
    /**
     * 视频
     * uid 用户UID
     * fileTitle 文件标题，支持中文
     * fileName 文件名，不支持中文
     * fileLink 文件链接
     * fileWidth 文件宽度
     * fileHeight 文件高度
     * del 0未删除，1已删除
     */

    /**
     * 视频历史
     * uid 用户UID
     * vid 视频ID
     * del 0未删除，1已删除
     */

    protected $table = 'video';
    protected $tHistory = 'video_history';
    protected $tZan = 'video_zan';

    public function __construct()
    {
    }

    //列表
    public function getIndex($page,$size,$ids)
    {
        $pageStart = ($page - 1) * $size;
        $models = DB::table($this->table)->where('del',0)
            ->whereNotIn('id',$ids)
            ->orderBy('id','asc')
            ->skip($pageStart)
            ->take($size)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $items = array();
        foreach ($models as $model) {
            $fileTitle = $model->fileTitle ? $model->fileTitle : '自定义';
            $fileName = $model->fileName ? $model->fileName : '';
            $fileImg = $model->fileImg ? $model->fileImg : '';
            $fileLink = $model->fileLink ? $model->fileLink : '';
            $ratio = $model->fileWidth ? $model->fileHeight / $model->fileWidth : 0;
            $items[] = [
                'videoId' => $model->id,
                'videoTitle' => $fileTitle,
                'videoName' => $fileName,
                'videoImg' => $fileImg,
//                'videoWidth' => $model->fileWidth,
//                'videoHeight' => $model->fileHeight,
                'videoRatio' => $ratio,
                'videoLink' => $fileLink,
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$items);
    }

    //单个
    public function getOne($uid,$ids)
    {
        $model = DB::table($this->table)->where('del',0)
            ->whereNotIn('id',$ids)
            ->orderBy('id','asc')
            ->first();
        if (!$model) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $fileTitle = $model->fileTitle ? $model->fileTitle : '自定义';
        $fileName = $model->fileName ? $model->fileName : '';
        $fileImg = $model->fileImg ? $model->fileImg : '';
        $fileLink = $model->fileLink ? $model->fileLink : '';
        $ratio = $model->fileWidth ? $model->fileHeight / $model->fileWidth : 0;
        $zanOne = DB::table($this->tZan)->where('del',0)
            ->where('uid',$uid)->where('vid',$model->id)
            ->first();
        $zan = $zanOne ? 1 : 0;
        $items = [
            'videoId' => $model->id,
            'videoTitle' => $fileTitle,
            'videoName' => $fileName,
            'videoImg' => $fileImg,
//                'videoWidth' => $model->fileWidth,
//                'videoHeight' => $model->fileHeight,
            'videoRatio' => $ratio,
            'videoLink' => $fileLink,
            'zan' => $zan,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$items);
    }

    /**
     * 扩增历史
     * @param $uid 用户ID
     * @param $vids 首页视频ID数组
     */
    public function setHistory($uid,$vids)
    {
        if (!$uid || !$vids) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastIds = array();
            foreach ($vids as $vid) {
                $model = DB::table($this->tHistory)
                    ->where('del',0)
                    ->where('uid',$uid)
                    ->where('vid',$vid)
                    ->first();
                if (!$model) {
                    $lastIds[] = DB::table($this->tHistory)
                        ->insertGetId([
                            'uid' => $uid,
                            'vid' => $vid,
                            'created_at' => time(),
                        ]);
                }
            }
            $dataArr = array(
                'lastIds' => $lastIds,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 删除历史
     * @param $uid 用户ID
     * @param $vids 首页视频ID集合
     */
    public function setHistoryDel($uid,$vids)
    {
        if (!$uid || !$vids) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->tHistory)->where('uid',$uid)
                ->whereIn('vid',$vids)->update([
                    'del' => 1,
                    'updated_at' => time(),
                ]);
            $dataArr = array(
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 点赞增加
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setZanAdd($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->tZan)->insertGetId([
                    'uid' => $uid,
                    'vid' => $vid,
                    'created_at' => time(),
                ]);
            $dataArr = array(
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 点赞移除
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setZanDel($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->tHistory)->where('uid',$uid)
                ->where('vid',$vid)->update([
                    'del' => 1,
                    'updated_at' => time(),
                ]);
            $dataArr = array(
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}